<?php
//abstract class State{
//    public abstract function  handle(Context $context);
//}
//
//class ConcreteStateA extends State{
//    public function handle(Context $context){
//        $context->state = new ConcreteStateB();
//    }
//}
//
//class ConcreteStateB extends State{
//    public function handle(Context $context)
//    {
//        $context->state = new ConcreteDecoratorA();
//    }
//}
//
//class Context{
//    private $state;
//
//    public function __construct($state)
//    {
//        $this->state = $state;
//    }
//
//    public function request(){
//        $this->state->handle(this);
//    }
//}
//
////客户端调用
//$context = new Context(new ConcreteDecoratorA());
//$context->request();
//$context->request();
//$context->request();
//$context->request();

//工作例子
abstract class State{
    public abstract function writeProgram(Work $w);
}

class ForenoonState extends State{
    public function writeProgram(Work $w){
        if($w->hour <12){
            echo '工作状态很好';
        }else{
            $w->setState(new NoonState());
            $w->writeProgram();
        }
    }
}

class NoonState extends State{
    public function writeProgram(Work $w){
        if($w->hour <13){
            echo '边吃边工作';
        }else{
            $w->setState(new AfternoonState());
            $w->writeProgram();
        }
    }
}

class AfternoonState extends State{
    public function writeProgram(Work $w){
        if($w->hour <17){
            echo '迷迷糊糊';
        }else{
            $w->setState(new EveningState());
            $w->writeProgram();
        }
    }
}

class EveningState extends State{
    public function writeProgram(Work $w){
        if($w->hour <21){
            echo '加班，疲劳至极';
        }else{
            $w->setState(new EveningState());
            $w->writeProgram();
        }
    }
}

class SleepState extends State{
    public function writeProgram(Work $w){
        echo '睡着了';
    }
}

class Work{
    private $state;
    public  $hour;
    public $finish;
    public function __construct()
    {
        $this->state = new ForenoonState();
    }

    public function setState($state)
    {
        $this->state = $state;
    }

    public function writeProgram(){
        $this->state->writeProgram($this);
    }
}

//客户端调用
$work = new Work();
$work->writeProgram();
$work->hour = 17;
$work->writeProgram();
$work->hour = 12;
$work->writeProgram();
$work->hour = 13;
$work->writeProgram();

